package ac.manchester.cs.afzal.tcm_processing.nlp;

import ac.manchester.cs.afzal.tcm_processing.sql_handler.SQL_Handler_Stemming_Impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import ac.manchester.cs.afzal.tcm_processing.miscellaneous.File_Manager_Impl;
import ac.manchester.cs.afzal.tcm_processing.miscellaneous.Vector_Builder_Impl;

/**
 * @author Hammad Afzal
 * @version 1.0
 * Last Modification Date: 24th April, 2012
 */
public class Stemming_Impl
{

    private String STEM_FILES_PATH = "STEM_FILES_PATH";
    private String database_url = "database_url";
    private String username = "username";
    private String password = "password";
    private String[] tables =
    {
        "Stem_Map_Ns", "Stem_Map_Vs"
    };
    private SQL_Handler_Stemming_Impl sqlHandler = new SQL_Handler_Stemming_Impl();
    private File_Manager_Impl fileManager = new File_Manager_Impl();
    Vector_Builder_Impl vector_builder = new Vector_Builder_Impl();

    public Stemming_Impl()
    {
        try
        {
            Properties props = new Properties();
            props.load(new FileInputStream("Term_Classification.properties"));
            STEM_FILES_PATH = props.getProperty(STEM_FILES_PATH);

            database_url = props.getProperty(database_url);
            username = props.getProperty(username);
            password = props.getProperty(password);

        }
        catch (IOException ex)
        {
            Logger.getLogger(Stemming_Impl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void populate_stems_table()
    {
        sqlHandler.initialize_parameters(database_url, username, password);
        sqlHandler.connect_db();
        sqlHandler.reset_tables(tables);


        Vector<String> occurrences_noun = new Vector<String>();
        Vector<String> stem_noun = new Vector<String>();
        Vector<String> occurrences_verb = new Vector<String>();
        Vector<String> stem_verb = new Vector<String>();

        String token_value = "";
        String stemmed_value = "";
        String tag_value = "";
        StringBuffer contents;

        File stem_files_dir = new File(STEM_FILES_PATH);
        for (String stem_file_name : stem_files_dir.list())
        {
            if ((stem_file_name.startsWith("."))) // excluding the hidden files/folders
                System.out.println("Hidden File Not Processed: " + stem_file_name);
            else
            {
                contents = fileManager.fileReader(STEM_FILES_PATH + "\\" + stem_file_name);
                Vector<String> noun_stem_tags = vector_builder.string_to_vector(contents.toString(), System.getProperty("line.separator"));

                /************************ This loop makes Vector of Real Terms and Occurrences and put in Real_Terms Database ****************************/
                for (String single_noun_stem_tag : noun_stem_tags)
                {
                    StringTokenizer st1 = new StringTokenizer(single_noun_stem_tag, " ");
                    token_value = st1.nextToken();
                    try
                    {
                        if (token_value != "EOS")
                        {
                            stemmed_value = st1.nextToken();
                            tag_value = st1.nextToken();
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.getStackTrace();
                    };

                    if ((token_value.length() > 2) && (token_value.length() < 45)
                            && !(token_value.startsWith("-")) && !(token_value.startsWith("%"))
                            && !(token_value.startsWith("*")) && !(token_value.startsWith("/"))
                            && !(token_value.equals(stemmed_value)))
                    {
                        if (tag_value.equals("N"))
                        {
                            if (!(occurrences_noun.contains(token_value)))
                            {
                                token_value.replaceAll("'", "");
                                stemmed_value.replaceAll("'", "");

                                occurrences_noun.add(token_value);
                                stem_noun.add(stemmed_value);
                            }
                        }
                        else if (tag_value.equals("V"))
                        {
                            if (!(occurrences_verb.contains(token_value)))
                            {
                                token_value.replaceAll("'", "");
                                stemmed_value.replaceAll("'", "");

                                occurrences_verb.add(token_value);
                                stem_verb.add(stemmed_value);
                            }
                        }
                    }
                }
            }
        }
        make_entry_in_database(occurrences_noun, occurrences_verb, stem_noun, stem_verb);
        sqlHandler.close_db();
    }

    private void make_entry_in_database(Vector<String> occurrences_noun, Vector<String> occurrences_verb,
            Vector<String> stem_noun, Vector<String> stem_verb)
    {

        for (int i = 0; i < occurrences_noun.size(); i++)
        {
            String[] doc_table1 =
            {
                "Noun", "Stem"
            };

            String[] doc_table_value1 =
            {
                occurrences_noun.elementAt(i).toString(),
                stem_noun.elementAt(i).toString()
            };

            sqlHandler.insert_table("Stem_Map_Ns", doc_table1, doc_table_value1);
        }

        for (int i = 0; i < occurrences_verb.size(); i++)
        {
            String[] doc_table1 =
            {
                "Verb", "Stem"
            };

            String[] doc_table_value1 =
            {
                occurrences_verb.elementAt(i).toString(),
                stem_verb.elementAt(i).toString()
            };
            sqlHandler.insert_table("Stem_Map_Vs", doc_table1, doc_table_value1);
        }

    }
}
